package com.dongdongshop.service.Impl;
import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.pojo.Cart;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.pojo.TbOrderItem;
import com.dongdongshop.service.CartService;
import com.dongdongshop.service.TbItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Service
@com.alibaba.dubbo.config.annotation.Service
public class CartServiceImpl implements CartService {
    @Reference
    private TbItemService tbItemService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public List<Cart> addCart(List<Cart> cartList, Long itemId, Integer num) {
        //1,根据itemId查询iten商品
         TbItem tbItem =  tbItemService.selectByItemId(itemId);
        //2,获取查询item商品商家
        String sellerId = tbItem.getSellerId();

        //3,根据上架Id判断当前购物车中有没有当前这个商家的购物车数据
        Cart cart = findCartBySellerId(cartList, sellerId);//这里调用了封装的方法
        //4,没有
        if(cart==null){
            //4.1,创建一个Cart对象
             cart =new Cart();//此时cart对象中的TbOrderItemList也是空的所以也需要new一个list集合
             //要将商家的ID放进去
             cart.setSellerId(sellerId);
            //4.2,将我们要添加的商品存到Cart对象的itemList里面
            List<TbOrderItem> orderItems =new ArrayList<>();
            TbOrderItem orderItem =new TbOrderItem();
            orderItem.setGoodsId(tbItem.getGoodsId());
            orderItem.setItemId(tbItem.getId());
            orderItem.setNum(num);
            orderItem.setPicPath(tbItem.getImage());
            orderItem.setPrice(tbItem.getPrice());
            orderItem.setSellerId(tbItem.getSellerId());
            orderItem.setTotalFee(new BigDecimal(tbItem.getPrice().doubleValue()*num));
            orderItem.setTitle(tbItem.getTitle());
            //放到明细列表list
            orderItems.add(orderItem);
            //明细列表放到购物车对象中
            cart.setOrderItemList(orderItems);
            //购物车对象放到购物车列表中
            cartList.add(cart);
        }else{
            //5,有
            List<TbOrderItem> orderItemList = cart.getOrderItemList();
            TbOrderItem orderItem = findOrderItemByItemId(orderItemList, itemId);
            //5.1判断原来这个商家的数据里面有没有这个商品
            if(orderItem==null){
                //5.1.1,没有   创建明细对象,然后网之前的list中追加一条
                orderItem.setGoodsId(tbItem.getGoodsId());
                orderItem.setItemId(tbItem.getId());
                orderItem.setNum(num);
                orderItem.setPicPath(tbItem.getImage());
                orderItem.setPrice(tbItem.getPrice());
                orderItem.setSellerId(tbItem.getSellerId());
                orderItem.setTotalFee(new BigDecimal(tbItem.getPrice().doubleValue()*num));
                orderItem.setTitle(tbItem.getTitle());
                //将创建好的对象放到原来的明细列表中
                orderItemList.add(orderItem);//引用传递
            }else{
                //5.1.2, 只要拿到原来那个对象,num+1 修改价格个数量
                orderItem.setNum(orderItem.getNum()+num);
                orderItem.setTotalFee(new BigDecimal(orderItem.getPrice().doubleValue()*orderItem.getNum()));
                //如果是减的话,当商品的数量<0的时候,我们就把这个商品从明细列表中移除
                if(orderItem.getNum().intValue()<=0){
                    orderItemList.remove(orderItem);
                }
                //如果明细为0了,我们要将这个商家从购物车中移除
                if(orderItemList.size()<=0){
                    cartList.remove(cart);
                }
            }

        }
         return cartList;
    }
    //往redis中添加购物车数据
    @Override
    public void addCartListToRedis(List<Cart> cartList, String username) {
        redisTemplate.boundHashOps("cartList").put(username,cartList);
    }
    //从redis中获取购物车数据
    @Override
    public List<Cart> findListCartFromRedis(String username) {
        List<Cart>  cartList = (List<Cart>) redisTemplate.boundHashOps("cartList").get(username);
        if(cartList==null){
            cartList = new ArrayList<>();//可以预防空指针
        }
        return cartList;
    }
    //cookie和redis合并数据
    @Override
    public List<Cart> merageCartList(List<Cart> cookieList, List<Cart> redisList) {
        for (Cart cart : cookieList) {
            List<TbOrderItem> orderItemList = cart.getOrderItemList();//遍历cooieList取出Cart对象中list集合,集合中是单个商家的购物车添加数据
            for (TbOrderItem orderItem : orderItemList) {
                redisList= addCart(redisList,orderItem.getItemId(),orderItem.getNum());//这里调用的是添加购物车的方法
            }
        }
        return redisList;
    }
    //封装方法
    public Cart findCartBySellerId(List<Cart> cartList,String sellerId) {
        for (Cart cart : cartList) {
            if (Objects.equals(cart.getSellerId(), sellerId)) {
                return cart;
            }
        }
        return null;
    }
    //根据itemid查询购物车列表中明细数据
    public TbOrderItem findOrderItemByItemId(List<TbOrderItem> orderItemList,Long itemId){
        for (TbOrderItem orderItem : orderItemList) {
            if(Objects.equals(orderItem.getItemId(),itemId)){
                return orderItem;
            }
        }
        return  null;
    }
}
